package com.ruoyi.house.service.impl;

import com.alibaba.fastjson2.util.DateUtils;
import com.baomidou.mybatisplus.plugins.Page;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.house.domain.*;
import com.ruoyi.house.dto.request.PageTbRoomInfoJzRequestDTO;
import com.ruoyi.house.dto.request.PageTbRoomInfoRequestDTO;
import com.ruoyi.house.dto.response.*;
import com.ruoyi.house.enums.BusinessCostTypesEnum;
import com.ruoyi.house.enums.ConditionType;
import com.ruoyi.house.enums.FinanceApplyStateEnum;
import com.ruoyi.house.mapper.*;
import com.ruoyi.house.mapper.dao.HouseInfoDao;
import com.ruoyi.house.mapper.dao.RoomInfoDao;
import com.ruoyi.house.service.TbRoomInfoService;
import com.ruoyi.house.utils.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("tbRoomInfoService")
public class TbRoomInfoServiceImpl extends BaseServiceImpl<TbRoomInfoMapper, TbRoomInfoEntity>
		implements TbRoomInfoService {

	@Resource
	private RoomInfoDao roomInfoDao;
	@Resource
	private TbRoomInfoMapper roomInfoMapper;
	@Resource
	private HouseInfoDao houseInfoDao;
	@Resource
	private CfContractMapper contractMapper;
	@Resource
	private CwReceiptPlanMapper receiptPlanMapper;
	@Resource
	private JrFactoringDetailMapper factoringDetailMapper;
	@Resource
	private JrFactoringMapper factoringMapper;
	@Resource
	private JrRentInstallmentMapper rentInstallmentMapper;
	@Resource
	private JrProductMapper productMapper;

	/**
	 * 分页 - 分散式
	 */
	@Override
	public PageTbRoomInfoRequestDTO pageListTbRoom(PageTbRoomInfoRequestDTO dto) {
		Page<PageTbRoomInfoRequestDTO> pagination = new Page<PageTbRoomInfoRequestDTO>(dto.getPageNum(),
				dto.getPageSize());
		List<PageTbRoomInfoResponseDTO> recordeList = roomInfoMapper.findPageByFs(pagination, dto);
		List<PageTbRoomInfoFsResponseDTO> res = null;
		if (recordeList.size() > 0) {
			res = this.pushRecordMini(recordeList,dto.getPreName());
		}
		dto.setTotalNum(pagination.getTotal());
		dto.setRecords(res);
		return dto;
	}

	/**
	 * @Description: 计算 - 分散式 - 房源赋值
	 * @return List<PageTbRoomInfoResponseDTO>
	 */
	private List<PageTbRoomInfoFsResponseDTO> pushRecordMini(List<PageTbRoomInfoResponseDTO> pageList,String searchName) {
		Set<String> roomKeySet = new HashSet<>();
		Set<String> miniKey = new HashSet<>();
		Map<String, List<PageTbRoomInfoMiniResponseDTO>> miniMap = new HashMap<>();
		List<PageTbRoomInfoFsResponseDTO> res = new ArrayList<>();
		List<PageTbRoomInfoFsResponseDTO> ho = new ArrayList<>();
		List<PageTbRoomInfoMiniResponseDTO> miniTemp;
		for (PageTbRoomInfoResponseDTO r : pageList) {
			String key = r.getPreId() + "-" + r.getHouseId();
			if (!roomKeySet.contains(key)) {
				PageTbRoomInfoFsResponseDTO d = new PageTbRoomInfoFsResponseDTO();
				StringBuffer buffer = new StringBuffer();
				if (StringUtils.isNotBlank(r.getBuildingNo())) {
					buffer.append(subHig(r.getBuildingNo(),searchName) + "栋");
				}
				if (StringUtils.isNotBlank(r.getUnitNo())) {
					buffer.append(subHig(r.getUnitNo(),searchName) + "单元");
				}
				if (StringUtils.isNotBlank(r.getRoomNo())) {
					buffer.append(subHig(r.getRoomNo(),searchName));
				}
				if (StringUtils.isNotBlank(buffer)) {
					r.setBdiNo(buffer.toString());
				}
				d.setRoomCount(r.getRoomCount());
				BeanUtils.copy(r, d);
				ho.add(d);
				roomKeySet.add(key);
			}
			// 初始计算房源
			PageTbRoomInfoMiniResponseDTO mini = this.getComputeRoomMini(r,searchName);
			if (null != mini.getRoomId()) {
				mini.setRoomCode(subHig(mini.getRoomCode(),searchName));
				String putKey = mini.getHouseId();
				List<PageTbRoomInfoMiniResponseDTO> list = miniMap.get(putKey);
				if (null != list) {
					// this.checkMini(mini, list);
					miniMap.get(putKey).add(mini);
				} else {
					miniTemp = new ArrayList<>();
					miniTemp.add(mini);
					miniMap.put(putKey, miniTemp);
				}
				miniKey.add(r.getRoomId());
			}
		}
		// 房产
		for (PageTbRoomInfoFsResponseDTO s : ho) {
			String getKey = s.getHouseId();
			List<PageTbRoomInfoMiniResponseDTO> list = miniMap.get(getKey);
			if (null != list && list.size() > 0) {
				s.setRoomCount(list.size());
				s.setRoomMni(list);
				s.setFindPreName(subHig(s.getPreName(),searchName));
				res.add(s);
			}
		}
		return res;
	}

	/**
	 * 
	 * @Description: 高亮显示 - 条件字段
	 * @param tag
	 * @param con
	 * @return String
	 */
	private String subHig(String tag, String con) {
		if (StringUtils.isNotBlank(con) && StringUtils.isNotBlank(tag)) {
			String temp = null;
			temp = tag;
			StringBuffer buf = new StringBuffer();
			int indexOf;
			String start = null;
			String end = null;
			while (temp.toUpperCase().indexOf(con.toUpperCase()) != -1) {
				indexOf = temp.indexOf(con);
				start = temp.substring(0, indexOf);
				end = temp.substring(indexOf + con.length(), temp.length());
				buf.append(start);
				buf.append("<font color='red' >" + con + "</font>");
				temp = end;
			}
			if (end == null) {
				buf.append(temp);
			} else {
				buf.append(end);
			}
			return buf.toString();
		} else {
			return tag;
		}
	}

	/**
	 * 
	 * @Description:删除 - 重复元素
	 * @param mini
	 * @param list
	 * 
	 */
	@SuppressWarnings("unused")
	private void checkMini(PageTbRoomInfoMiniResponseDTO mini, List<PageTbRoomInfoMiniResponseDTO> list) {
		for (int i = 0; i < list.size(); i++) {
			if (mini.getRoomId().equals(list.get(i).getRoomId())) {
				list.remove(i);
				break;
			}
		}
	}

	/**
	 * 
	 * @Description: 计算 - 房源状态
	 * @return PageTbRoomInfoMiniResponseDTO
	 */
	private PageTbRoomInfoMiniResponseDTO getComputeRoomMini(PageTbRoomInfoResponseDTO pdto,String searchName) {
		PageTbRoomInfoMiniResponseDTO res = new PageTbRoomInfoMiniResponseDTO();
		List<Map<String, Object>> dList;
		Map<String, Object> dmap;
		int roomStatus = pdto.getRoomStatus();
		if (1 == roomStatus) { // 已租出
			dList = new ArrayList<>();
			int cEndDate = 0;
			int rcDate = 0;
			if (null != pdto.getCEndDate()) {
				cEndDate = Integer.parseInt(pdto.getCEndDate());// 合同天数
			}
			if (null != pdto.getRcDates()) {
				rcDate = Integer.parseInt(pdto.getRcDates());// 付款天数
			}
			// 计算租金日期
			if (0 < rcDate && rcDate <= 10) {
				dmap = new HashMap<>();
				dmap.put("FangtaiType", ConditionType.SHOUFEI.getIndex()); // 收费
				dmap.put("total", Math.abs(rcDate));
				dList.add(dmap);
			} else if (rcDate < 0) {
				dmap = new HashMap<>();
				dmap.put("FangtaiType", ConditionType.QIANFEI.getIndex()); // 欠费
				dmap.put("total", Math.abs(rcDate));
				dList.add(dmap);
			}
			// 计算合同
			if (0 < cEndDate && cEndDate <= 30) {
				dmap = new HashMap<>();
				dmap.put("FangtaiType", ConditionType.KUAIDAO.getIndex()); // 快到
				dmap.put("total", Math.abs(cEndDate));
				dList.add(dmap);
				if (pdto.getCheckTerm() == 0) { // 可续约
					pdto.setCheckTerm(6);
				} else if (cEndDate < 0) {
					dmap = new HashMap<>();
					dmap.put("FangtaiType", ConditionType.DAOQI.getIndex()); // 到期
					dmap.put("total", Math.abs(cEndDate));
					dList.add(dmap);
				}
			}
			if (null == pdto.getCEndDate() && null == pdto.getRcDates()) {
				dmap = new HashMap<>();
				dmap.put("FangtaiType", ConditionType.YIDENGJI.getIndex()); // 已登记
				dList.add(dmap);
				pdto.setCheckRegister(1);
			}
			pdto.setMiniDetail(dList);

		} else if (2 == roomStatus) {// 装修中
			dList = new ArrayList<>();
			dmap = new HashMap<>();
			double finishDate = 0l;
			if (null != pdto.getFinishDate()) {
				finishDate = Integer.parseInt(pdto.getFinishDate());
			}
			if (finishDate != 0) {
				dmap.put("FangtaiType", ConditionType.ZHUANGXIU.getIndex());// 房态类型 装修
				dmap.put("total", Math.abs(finishDate));// 天数
				dList.add(dmap);
			}
			pdto.setMiniDetail(dList);
		} else if (0 == roomStatus) { // 0 默认 待租中（空置）
			dList = new ArrayList<>();
			dmap = new HashMap<>();
			Integer vacancyDate = 0;
			if (null != pdto.getVacancyDate()) {
				vacancyDate = Integer.parseInt(pdto.getVacancyDate());
			}
			if (vacancyDate != 0) {
				dmap.put("FangtaiType", ConditionType.KONGZHI.getIndex());// 房态类型 空置
				dmap.put("total", Math.abs(vacancyDate));// 天数
				dList.add(dmap);
				pdto.setLeisureDate(Math.abs(vacancyDate));
			}
			pdto.setLeisureFlag(1);
			pdto.setMiniDetail(dList);
		}
		BeanUtils.copy(pdto, res);
		res.setTenantName(subHig(res.getTenantName(), searchName));
		return res;
	}

	/**
	 * 
	 * @Description: 查询 - 房源详情
	 *            房源ID
	 * @return DetailTbRoomResponseDTO
	 */
	@Override
	public DetailTbRoomResponseDTO getRoomInfoDetailById(String roomId, Long beforeCfContractId) {

		DetailTbRoomResponseDTO result = new DetailTbRoomResponseDTO();
		String houseId = "";
		String buildingName = "";// 集中式房源-楼栋名称
		int floor = 0;// 集中式房源-楼层
		String roomCode = "";// 房源编号
		String cfContractNo = "";// 出房合同编号
		if (StringUtils.isNotBlank(roomId)) {
			// 房源表TB_ROOM_INFO中查询数据
			TbRoomInfoEntity roomInfo = roomInfoMapper.selectById(roomId);
			if (roomInfo != null) {
				result.setId(roomInfo.getId());
				result.setRoomCode(roomInfo.getRoomCode());
				result.setStatus(roomInfo.getStatus());// 获取房源状态
				result.setRental(roomInfo.getRental());// 获取房租
				houseId = roomInfo.getHouseId();
				buildingName = roomInfo.getBuildingName();// 集中式房源-楼栋名称
				floor = roomInfo.getFloor();// 集中式房源-楼层
				roomCode = roomInfo.getRoomCode();// 房源编号
			}
			// 根据房源表TB_ROOM_INFO表中的house_id到房产信息表TB_HOUSE_INFO表中查询相关数据
			if (StringUtils.isNotBlank(houseId)) {
				HouseInfoEntity houseInfo = houseInfoDao.getHouseInfoByHouseId(houseId);
				String addr = "";// 房源地址，如12栋2单元101
				Integer type = houseInfo.getType();// 0：分散式;1：集中式
				String buildingNo = houseInfo.getBuildingNo(); // 栋
				String unitNo = houseInfo.getUnitNo();// 层
				if (StringUtils.isNotBlank(buildingNo) && StringUtils.isNotBlank(unitNo)) {
					if (type != null) {
						if (type == 0) {// 该房源为分散式房源
							addr = buildingNo + "栋" + unitNo + "单元" + roomCode;
						} else if (type == 1) { // 该房源为集中式房源
							addr = buildingName + "栋" + floor + "层" + roomCode;
						}
					}
					result.setAddr(addr);
				}
				if (null != houseInfo.getRoom()) {
					result.setRoom(houseInfo.getRoom());// X室Y厅中的X
				}
				if (null != houseInfo.getHall()) {
					result.setHall(houseInfo.getHall());// X室Y厅中的Y
				}
				if (null != houseInfo.getProportion()) {
					result.setProportion(houseInfo.getProportion());// 面积
				}
			}
			// 根据房源id在t_cf_contract表中查询到
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("roomId", roomId.toString());
			List<CfContractEntity> nowContractInfo = contractMapper.selectRoomId(param);// 查询最新合同
			if (nowContractInfo != null && nowContractInfo.size() > 0) {
				result.setCfContractId(nowContractInfo.get(0).getId());
			}
			if (null != beforeCfContractId && beforeCfContractId > 0) {
				CfContractEntity beforeCfContract = contractMapper.selectById(beforeCfContractId);// 当前合同
				cfContractNo = beforeCfContract.getCfContractNo();// 出房合同编号
				result.setFreezeStatus(beforeCfContract.getFreezeStatus());// 冻结状态
				result.setCfContractNo(beforeCfContract.getCfContractNo());// 设置出房合同编号
				result.setTenantPhone(beforeCfContract.getTenantPhone());// 设置租客手机号
				result.setStartDate(beforeCfContract.getStartDate());// 设置租约期限开始日
				result.setEndDate(beforeCfContract.getEndDate());// 设置租约期限结束日
				result.setContractState(beforeCfContract.getContractState());// 设置合同状态,"1:草稿 2:已提交 3:已解约 4:已结束 9:作废"
				result.setPaymentMortgage(beforeCfContract.getDepositType());// 付款方式-押
				result.setPaymentPay(beforeCfContract.getPaymentType());// 付款方式-付
				// 金融服务
				Boolean rentInstallmentFlg = beforeCfContract.getRentInstallmentFlg();// 分期
				Boolean zyblFlg = beforeCfContract.getZyblFlg();// 保理
				Map<String, Object> jrMap = new HashMap<>();
				if (rentInstallmentFlg) {
					jrMap.put("cf_contract_id", beforeCfContract.getId());
					jrMap.put("apply_state", FinanceApplyStateEnum.FSPASS.getIndex());
					jrMap.put("del_flg", 0);
					List<JrRentInstallmentEntity> iList = rentInstallmentMapper.selectByMap(jrMap);
					if (iList.size() > 0) {
						JrRentInstallmentEntity i = iList.get(0);
						JrProductEntity p = productMapper.selectById(i.getJrProductId());
						result.setJrName(p.getJrProductName());
						result.setJrNo(i.getRentInstallmentOrderNo());
						result.setJrTime(i.getCreateTime());
						result.setJrMoney(i.getReLeaseCount().toString());
					}
				} else if (zyblFlg) {
					jrMap.put("cf_contract_id", beforeCfContract.getId());
					jrMap.put("del_flg", 0);
					List<JrFactoringDetailEntity> fdList = factoringDetailMapper.selectByMap(jrMap);
					if (fdList.size() > 0) {
						JrFactoringDetailEntity fd = fdList.get(0);
						JrFactoringEntity f = factoringMapper.selectById(fd.getFactoringId());
						if (FinanceApplyStateEnum.FSPASS.getIndex() == f.getApplyState()) {
							JrProductEntity p = productMapper.selectById(f.getJrProductId());
							result.setJrName(p.getJrProductName());
							result.setJrNo(f.getFactoringOrderNo());
							result.setJrTime(f.getCreateTime());
							result.setJrMoney(fd.getFactoringCreditMoney().toString());
						}
					}
				}
			}
			// 根据出房合同编号cfContractNo查询收款计划表t_cw_receipt_plan
			if (StringUtils.isNotBlank(cfContractNo)) {
				BigDecimal rentArrears = BigDecimal.ZERO;// 欠费房租金额
				BigDecimal otherArrears = BigDecimal.ZERO;// 其它欠费
				String today = DateUtils.format(new Date());
				String nextRentDate = new String();// 租金下期付款日
				Map<String, Object> paramForReceipt = new HashMap<String, Object>();
				paramForReceipt.put("contractNo", cfContractNo);// 合同编号
				paramForReceipt.put("skStatus", 1); // 收款状态，1:未收款 2：已收款 3：已中止
				paramForReceipt.put("collectionDate", new Date());// 预定收款日期
				List<RespForRentArrears> queryForRentArrears = receiptPlanMapper.queryForArrears(paramForReceipt);
				if (queryForRentArrears != null && queryForRentArrears.size() > 0) {
					rentArrears = BigDecimal.ZERO;// 欠费房租金额
					otherArrears = BigDecimal.ZERO;// 其它欠费
					today = DateUtils.format(new Date());
					nextRentDate = new String();// 租金下期付款日
					for (int index = 0; index < queryForRentArrears.size(); index++) {
						RespForRentArrears temp = queryForRentArrears.get(index);
						Integer feeType = temp.getItemType();// 款项
						BigDecimal paymentAmount = temp.getPaymentAmount();// 交付金额
						String strCollectionDate = DateUtils.format(temp.getCollectionDate()); // 预定交付时间
						if (strCollectionDate.compareTo(today) < 0) {
							if (feeType == BusinessCostTypesEnum.ZUJIN.getIndex()) {
								rentArrears = rentArrears.add(paymentAmount); // 欠费金额累计
							} else {
								otherArrears = otherArrears.add(paymentAmount);// 其它欠费
							}
						}
						if (strCollectionDate.compareTo(nextRentDate) < 0) {
							nextRentDate = strCollectionDate;// 下期付款日
						}
					}
				}
				result.setRentArrears(rentArrears);
				result.setOtherArrears(otherArrears);
				if (StringUtils.isNotBlank(nextRentDate)) {
					result.setNextRentDate(nextRentDate);
				}
			}
		} else {
			return null;
		}
		return result;
	}

	@Override
	public void UpdateTbRoomInfo(RoomInfoEntity entity) {
		roomInfoDao.update(entity);
	}

	@Override
	public RoomInfoEntity getRoomInfoById(String id) {
		Map<String, Object> param = new HashMap<>();
		param.put("rid", id);
		return roomInfoDao.findById(param);
	}

	@Override
	public HouseInfoEntity getHouseInfo(String id) {
		return houseInfoDao.getHouseInfoByHouseId(id);
	}

	@Override
	public void updateHouse(HouseInfoEntity entity) {
		houseInfoDao.update(entity);
	}

	/**
	 * 
	 * @Description: 计算 - 时间差
	 * @param wornDate
	 * @param nowDate
	 * @return int
	 */
	@SuppressWarnings("unused")
	private int computDate(Date nowDate, Date wornDate) {
		int count = 0;
		if (null == wornDate || null == nowDate) {
			return count;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sdf.format(wornDate);
		try {
			wornDate = sdf.parse(sdf.format(wornDate));
			nowDate = sdf.parse(sdf.format(nowDate));
			long d = (nowDate.getTime() - wornDate.getTime() + 1000000) / (3600 * 24 * 1000);
			count = (int) d;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	/**
	 * 分页 - 集中式
	 */
	@Override
	public PageTbRoomInfoJzRequestDTO pageListTbRoomByZj(PageTbRoomInfoJzRequestDTO dto) {
		Page<PageTbRoomInfoRequestDTO> pagination = new Page<PageTbRoomInfoRequestDTO>(dto.getPageNum(),
				dto.getPageSize());
		List<PageTbRoomInfoResponseDTO> recordeList = roomInfoMapper.findPageByJz(pagination, dto);
		if (recordeList.size() > 0) {
			dto.setHouseId(recordeList.get(0).getHouseId());
			recordeList = roomInfoMapper.findPageListDetailByJz(dto);
		}
		List<PageTbRoomInfoJzGyResponseDTO> res = null;
		if (recordeList.size() > 0) {
			res = this.pushJzRecord(recordeList,dto.getPreName());
		}
		dto.setTotalNum(pagination.getTotal());
		dto.setRecords(res);
		return dto;
	}

	/**
	 * 
	 * @Description: 计算 - 集中式房源赋值
	 * @param minis
	 * @return List<PageTbRoomInfoJzGyResponseDTO>
	 */
	private List<PageTbRoomInfoJzGyResponseDTO> pushJzRecord(List<PageTbRoomInfoResponseDTO> minis,String searchName) {
		Map<String, Integer> countMap = new HashMap<>();
		Map<String, Integer> numMap = new HashMap<>();
		List<PageTbRoomInfoJzGyResponseDTO> gy = new ArrayList<>(); // 公寓s
		List<PageTbRoomInfoJzLdResponseDTO> ld = new ArrayList<>(); // 楼栋s
		List<PageTbRoomInfoJzLcResponseDTO> lc = new ArrayList<>(); // 楼层s
		List<PageTbRoomInfoMiniResponseDTO> fy; // 房源s
		PageTbRoomInfoJzGyResponseDTO g;
		PageTbRoomInfoJzLdResponseDTO d;// 楼栋
		PageTbRoomInfoJzLcResponseDTO c;// 楼层
		Set<String> gySet = new HashSet<>();
		Set<String> ldSet = new HashSet<>();
		Set<String> lcSet = new HashSet<>();
		Set<String> mkey = new HashSet<>();
		Map<String, List<PageTbRoomInfoJzLdResponseDTO>> ldMap = new HashMap<>();
		Map<String, List<PageTbRoomInfoJzLcResponseDTO>> lcMap = new HashMap<>();
		Map<String, List<PageTbRoomInfoMiniResponseDTO>> fyMap = new HashMap<>();
		for (PageTbRoomInfoResponseDTO m : minis) {
			String gyConKey = m.getHouseId();
			// 初始公寓
			if (!gySet.contains(gyConKey)) {
				g = new PageTbRoomInfoJzGyResponseDTO();
				BeanUtils.copy(m, g);
				g.setFindFlatName(subHig(g.getFlatName(), searchName));
				g.setPreName(subHig(g.getPreName(), searchName));
				gy.add(g);
				gySet.add(gyConKey);
			}
			// 初始楼栋
			String ldConsKey = m.getHouseId() + "-" + m.getBuildingName();
			if (!ldSet.contains(ldConsKey)) {
				d = new PageTbRoomInfoJzLdResponseDTO();
				d.setBuildingName(subHig(m.getBuildingName(),searchName));
				d.setHouseId(m.getHouseId());
				ld.add(d);
				ldSet.add(ldConsKey);
			}
			// 初始楼层
			String lcConsKey = m.getHouseId() + "-" + m.getBuildingName() + "-" + m.getFloor();
			if (!lcSet.contains(lcConsKey)) {
				c = new PageTbRoomInfoJzLcResponseDTO();
				c.setBuildingName(m.getBuildingName());
				c.setFloor(subHig(m.getFloor()+"",searchName));
				c.setHouseId(m.getHouseId());
				lc.add(c);
				lcSet.add(lcConsKey);
			}
			// 初始计算房源
			if (!mkey.contains(m.getRoomId()) || null != m.getRcDates()) {
				PageTbRoomInfoMiniResponseDTO mini = this.getComputeRoomMini(m,searchName);
				mini.setRoomCode(subHig(mini.getRoomCode(), searchName));
				if (null != mini.getRoomId()) {
					String putKey = mini.getHouseId() + "-" + mini.getBuildingName() + "-" + mini.getFloor();
					List<PageTbRoomInfoMiniResponseDTO> list = fyMap.get(putKey);
					if (null != list) {
						// this.checkMini(mini, list);
						fyMap.get(putKey).add(mini);

					} else {
						fy = new ArrayList<>();
						fy.add(mini);
						fyMap.put(putKey, fy);
					}
					// 记录时间
					String countKey = mini.getHouseId() + "-" + mini.getBuildingName();
					Integer count = countMap.get(countKey);
					if (null == count) {
						if (!mkey.contains(m.getRoomId())) {
							countMap.put(countKey, 1);
						}
					} else {
						if (!mkey.contains(m.getRoomId())) {
							count++;
							countMap.put(countKey, count);
						}
					}
					if (mini.getLeisureFlag() == 1) {
						Integer num = numMap.get(countKey);
						if (null == num) {
							numMap.put(countKey, 1);
						} else {
							num++;
							numMap.put(countKey, num);
						}
					}
				}
				mkey.add(m.getRoomId());
			}
		}
		List<PageTbRoomInfoJzLcResponseDTO> lcTemp;
		// 计算楼层
		for (PageTbRoomInfoJzLcResponseDTO l : lc) {
			String getkKey = l.getHouseId() + "-" + l.getBuildingName() + "-" + l.getFloor();
			List<PageTbRoomInfoMiniResponseDTO> list = fyMap.get(getkKey);
			if (null != list && list.size() > 0) {
				l.setMiniDetail(list);
				String key = l.getHouseId() + "-" + l.getBuildingName();
				if (null != lcMap.get(key)) {
					lcMap.get(key).add(l);
				} else {
					lcTemp = new ArrayList<>();
					lcTemp.add(l);
					lcMap.put(key, lcTemp);
				}
			}
		}
		List<PageTbRoomInfoJzLdResponseDTO> ldTemp;
		// 计算楼栋
		for (PageTbRoomInfoJzLdResponseDTO ds : ld) {
			String getKey = ds.getHouseId() + "-" + ds.getBuildingName();
			List<PageTbRoomInfoJzLcResponseDTO> list = lcMap.get(getKey);
			if (null != list && list.size() > 0) {
				ds.setMiniDetail(list);
				if (null != countMap.get(getKey)) {
					ds.setTotal(countMap.get(getKey));
				}
				if (null != numMap.get(getKey)) {
					ds.setVacancyNum(numMap.get(getKey));
				}

				if (null != ldMap.get(ds.getHouseId())) {
					ldMap.get(ds.getHouseId()).add(ds);
				} else {
					ldTemp = new ArrayList<>();
					ldTemp.add(ds);
					ldMap.put(ds.getHouseId(), ldTemp);
				}
			}
		}
		// 计算公寓
		for (PageTbRoomInfoJzGyResponseDTO gs : gy) {
			List<PageTbRoomInfoJzLdResponseDTO> list = ldMap.get(gs.getHouseId());
			if (null != list && list.size() > 0) {
				gs.setLdDetail(list);
			}
		}

		return gy;
	}

}
